<template>
	<HyCard title="基本信息">
		<HyForm :hyFormDefault="hyFormDefault" :hyFormConfig="hyFormConfig" :hyFormFields="hyFormFields" ref="hyFormRef" />
		<AppTable v-if="!showOtherTable" :routerParams="props.routerParams" ref="appTableRef" :appCategory="appCategory" />
		<OtherTable v-if="showOtherTable" :routerParams="props.routerParams" ref="otherTableRef" />
		<div class="mt-5"></div>
		<el-alert :closable="false" title="通知内容预览" type="success" :description="noticeContent" />
	</HyCard>
</template>
<script setup lang="ts">
import HyForm from '/@/components/HyForm/index.vue';
import HyCard from '/@/components/HyCard/index.vue';
import { useDict, useTreeDict } from '/@/hooks/dict';
import AppTable from '../AppTable/index.vue';
import OtherTable from '../OtherTable/index.vue';
import { ElMessage } from 'element-plus';

const messageItem = ref({
	meetingName: '',
	reviewTime: '',
	estimatedReviewTime: '',
	arrivalTime: '',
	contactPerson: '',
	contactInformation: '',
	reviewType: '',
	appCategory: '',
	reviewAddress: '',
});
const formType: string = inject('formType', 'submit');
// 定义字典
const { review_type } = useDict('review_type');
const { app_category } = useTreeDict('app_category');

//配置基本信息表单
const hyFormFields = computed(() => {
	return {
		id: {
			type: 'input',
			hidden: true,
		},
		meetingName: {
			label: '会议名称',
			type: 'textarea',
			required: true,
			rows: 2,
			maxlength: 200,
			input: (value: string) => setMessageItem(value, 'meetingName'),
		},
		reviewTime: {
			label: '评审时间',
			type: 'date',
			picker: 'datetime',
			change: (value: string) => setMessageItem(value, 'reviewTime'),
			required: true,
		},
		estimatedReviewTime: {
			label: '预计评审时间',
			type: 'inputNumber',
			change: (value: string) => setMessageItem(value, 'estimatedReviewTime'),
			required: true,
		},
		arrivalTime: {
			label: '评委到场时间',
			type: 'date',
			change: (value: string) => setMessageItem(value, 'arrivalTime'),
			picker: 'datetime',
			required: true,
		},
		contactPerson: {
			label: '联系人',
			required: true,
			type: 'input',
			input: (value: string) => setMessageItem(value, 'contactPerson'),
		},
		contactInformation: {
			label: '联系方式',
			type: 'input',
			required: true,
			rules: [
				{ required: true, message: '请输入联系电话', trigger: 'blur' },
				{
					message: '请输入正确的11位手机号',
					pattern: /^1[3-9]\d{9}$/,
					trigger: 'blur',
				},
			],
			input: (value: string) => setMessageItem(value, 'contactInformation'),
		},
		reviewType: {
			label: '评审类型',
			type: 'select',
			required: true,
			disabled: props.routerParams.operateType !== 'add',
			change: (value: string) => setMessageItem(value, 'reviewType'),
			options: review_type.value,
			showAllLevels: true,
		},
		appCategoryList: {
			label: '应用类别',
			type: 'cascader',
			disabled: props.routerParams.operateType !== 'add',
			required: true,
			showAllLevels: true,
			change: (value: string) => setMessageItem(value, 'appCategory'),
			options: app_category.value,
		},
		reviewAddress: {
			label: '评审地点',
			type: 'textarea',
			required: true,
			input: (value: string) => setMessageItem(value, 'reviewAddress'),
			maxlength: 200,
		},
	};
});

const hyFormRef = ref();
//动态表单配置
const hyFormConfig = computed(() => {
	return {
		labelWidth: '120px',
		type: formType,
	};
});
const props = defineProps({
	detailData: {
		type: Object,
		default: () => {
			return {};
		},
	},
	reviewAppData: {
		type: Array,
		default: () => {
			return [];
		},
	},
	routerParams: {
		type: Object,
		default: () => {
			return {};
		},
	},
});

const appTableRef = ref();
const hyFormDefault = ref({});
const noticeContent = ref();
const appCategory = ref('');
const reviewTypeName = ref('');
const showOtherTable = ref(false);
//配置描述
const setMessageItem = (value: any, type: string) => {
	if (type === 'reviewType') {
		reviewTypeName.value = review_type.value.find((item: any) => item.value === value)?.label;
	}
	if (type === 'appCategory') {
		appCategory.value = value[value.length - 1];
		//判断应用类别是否为系统外
		if (value[value.length - 1] === '3') {
			showOtherTable.value = true;
		} else {
			showOtherTable.value = false;
		}
	}
	messageItem.value = {
		...messageItem.value,
		[type]: value,
	};
	noticeContent.value = `您好，这里是数字化应用全周期管理专家抽取系统，邀请您参加：${messageItem.value.meetingName} 评审活动。评审时间：${messageItem.value.reviewTime}，预计评审耗时：${messageItem.value.estimatedReviewTime} ，评审地点：${messageItem.value.reviewAddress} ，评审内容：${reviewTypeName.value}。请在身体健康的前提下再确认是否参加评审话动，请充分考虑时间安排。联系人：${messageItem.value.contactPerson}，联系方式：${messageItem.value.contactInformation}。`;
};
watch(
	() => props.detailData,
	(nv1: any) => {
		hyFormDefault.value = nv1;
		const fields = Object.keys(nv1);
		fields.forEach((item) => {
			setMessageItem(nv1[item], item);
		});
	},
	{ immediate: true }
);
watch(
	() => props.reviewAppData,
	(nv1: any) => {
		if (nv1 && nv1.length > 0) {
			nextTick(() => {
				if (props.detailData.appCategory !== '3') {
					appTableRef.value.tableData = nv1;
				} else {
					props.reviewAppData.forEach((item: any) => {
						otherTableRef.value.add({ ...item, disabled: true });
					});
				}
			});
		}
	},
	{ immediate: true, deep: true }
);
const otherTableRef = ref();
const check = async () => {
	const result1 = await hyFormRef.value.check();
	if (!result1) return false;
	//系统内应用是否添加了数据
	if (['0', '1', '2'].includes(appCategory.value)) {
		if (appTableRef.value.tableData.length === 0) {
			ElMessage.error('请选择应用');
			return false;
		}
	}
	//系统外应用是否添加了数据
	if (appCategory.value === '3') {
		if (otherTableRef.value.form.tableData.length === 0) {
			ElMessage.error('请新增应用');
			return false;
		} else {
			const result = await otherTableRef.value.check();
			if (!result) return false;
		}
	}
	return true;
};
const getData = () => {
	let reviewAppList = [];
	if (appCategory.value === '3') {
		reviewAppList = otherTableRef.value.form.tableData;
	} else {
		reviewAppList = appTableRef.value.tableData;
	}
	return {
		baseInfo: {
			...hyFormRef.value.hyForm,
			noticeContent: noticeContent.value,
		},
		reviewAppList,
	};
};
defineExpose({ getData, check });
</script>
<style scoped lang="scss"></style>
